package panels;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import classes.Matrix;

public class MatrixPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	public MatrixPanel() {
		setBorder(new TitledBorder(null, "Matrix", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new BorderLayout(0, 0));
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"1", "0", "0", "0"},
				{"0", "1", "0", "0"},
				{"0", "0", "1", "0"},
				{"0", "0", "0", "1"},
			},
			new String[] {
				"New column", "New column", "New column", "New column"
			}
		));
		table.setFont(new Font("Courier New", Font.BOLD, 15));
		table.setFillsViewportHeight(true);
		table.setCellSelectionEnabled(true);
		table.setColumnSelectionAllowed(true);
		add(table, BorderLayout.CENTER);
		
		setMatrix(Matrix.identity());
	}
	
	public Matrix	getMatrix() {
		int	row, col;
		Matrix	M = Matrix.blank();
		
		for(row=0; row<4; row++) {
			for(col=0; col<4; col++) {
				try {
					float value = Float.parseFloat(table.getModel().getValueAt(row, col).toString());
					M.set(col, row, value);
				} catch (Exception e) {
					
				}
			}
		}
		
		return M;
	}
	
	public void setMatrix(Matrix M) {
		int	row, col;

		for(row=0; row<4; row++) {
			for(col=0; col<4; col++) {
				try {
					table.getModel().setValueAt(new Float(M.get(col, row)), row, col);
				} catch (Exception e) {
					
				}
			}
		}
	}
}
	